<?php
		require "../common/db.inc";
		$conn = new mysqli($host, $username, $password, $dbname);	
		$conn->query("set names 'utf8'");		
		//check connection
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
				
		$key = $_GET["keyword"];
		$sql = "call sp_CountUserSearchResult('%$key%');";
		$result = $conn->query($sql);
		$obj = $result->fetch_object();
		$num_rows = $obj->num_of_users;
		
		
		if(isset ( $_GET["page"] ))
			$page = $_GET["page"];
		else
			$page = 1;
			
		$rows_per_page = 10;
		$pages_per_page = 5;
		$offset = ($page-1)*$rows_per_page;	
		
		
		$conn = new mysqli($host, $username, $password, $dbname);	
		$conn->query("set names 'utf8'");
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}	
		
		$sql = "call sp_SearchUserByNameOrEmail('%$key%', $offset, $rows_per_page);";
		$result = $conn->query($sql);
		
		echo "<br><a href='usermanage.php'>:: Quay lại danh sách thành viên ::</a><br><br>";
			
		echo "<table  width='80%' border='1' cellspacing='0' cellpadding='5px'>";
		echo "<tr style='background-color:#AACCEE' >";
		echo "<td>ID</td>";
		echo "<td>Username</td>";
		echo "<td>Email</td>";
		echo "<td>Mana</td>";
		echo "<td>&nbsp; </td>";	
		echo "</tr>";
		
		while ($row = $result->fetch_row()) 
		{
			$id	= $row[0];
			$username = $row[1];
			$email = $row[3];
			$mana = $row[5];
			$status = $row[6];
			
		echo "<tr>";
		echo "<td>$id</td>";
		echo "<td>$username</td>";		
		echo "<td>$email</td>";		
		echo "<td>$mana</td>";
		if($status == 1)
			echo "<td><a href='#' onClick='Active(this, $id); return false;'>Vô hiệu</a></td>";
		else
			echo "<td><a href='#' onClick='Active(this, $id); return false;'>Kích hoạt</a></td>";
		echo "</tr>";
		}
		
		echo "</table>";
		
		include "../common/paging_ajax.php";
		$listpage = paging("usermanage.php", $rows_per_page, $pages_per_page, $num_rows, $page, "");
		
		echo "<br>".$listpage;
		
		// free result set		
		$result->close();
		$conn->close();			
?>